$(function(){
// 下拉刷新
mui.init({
  pullRefresh : {
    container:".mui-scroll-wrapper",//下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
    down : {
      auto: true,
      callback :function(){
          $.ajax({
            url:'/cart/queryCart',
            type:'get',
            success:function(info){
              console.log(info);
              setTimeout(function(){
                if(info.error){
                  location.href = "login.html?backUrl="+location.href;
                }
                $(".mui-table-view").html(template("tmp",{list:info}));
                mui('.mui-scroll-wrapper')
                .pullRefresh()
                .endPulldownToRefresh();
              },1000)
            }
          })
      } 
    }
  }
});
// 删除
$(".mui-table-view").on("tap",".btn_delete",function(){
  var id = $(".btn_delete").data("id");
  $.ajax({
    url:'/cart/deleteCart',
    type:'get',
    data:{
      id:id
    },
    success:function(info){
      mui('.mui-scroll-wrapper')
      .pullRefresh()
      .pulldownLoading();
    }
  })
  
})

// 编辑
$(".mui-table-view").on("tap",".btn_edit",function(){
  console.log(1);
  var obj = this.dataset;
  console.log(obj);
  var html = template("tmp1",obj);
  html = html.replace(/\n/g, '')
  mui.confirm(html, '编辑商品', ['确定', '取消'], function (e) {
    if(e.index === 0){
      var id = obj.id;
      var num = $('.mui-numbox-input').val();
      var size = $('.pro_size span.active').text();
      $.ajax({
        url:'/cart/updateCart',
        type:'post',
        data:{
          id:id,
          num:num,
          size:size
        },
        success:function(info){
          console.log(info);
          if (info.success) {
            mui('.mui-scroll-wrapper')
              .pullRefresh()
              .pulldownLoading()
          }
        }
      })
    }
  })
  
  

   
 // 让尺码能够选择
 $('.pro_size span').on('click', function() {
  $(this)
    .addClass('active')
    .siblings()
    .removeClass('active')
})
// 初始化numbox
mui('.mui-numbox').numbox()
})

// 算钱
$('#OA_task_2').on('change', '.ck', function() {
var total = 0
// 获取到所有被选中的checkbox， 拿到自定义的num和 price
var $checked = $('.ck:checked')
$checked.each(function() {
  total += $(this).data('num') * $(this).data('price')
})
total = total.toFixed(2)
console.log(total)
$('.total').html(total)
})

})